/**
 * @Author:Geln Yang
 * @Created:2008-10-21
 * $ Revision: 1.0 $
 * $ Author: Geln Yang $
 * $ Date: 2008-10-21 $
 */
package org.geln.mail;

import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
 * @Author:Geln Yang
 * @Created:2008-10-21
 */
public class MailSender {
	private static Log logger = LogFactory.getLog(MailSender.class);

	private JavaMailSender javaMailSender;

	private String from;

	public void sendMail(String[] to, String[] cc, String subject, String text) throws MessagingException {

		try {
			MimeMessage message = javaMailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(message, true);

			BodyPart mdp = new MimeBodyPart();
			mdp.setContent(text, "text/html;charset=utf-8");
			Multipart mm = new MimeMultipart();
			mm.addBodyPart(mdp);
			message.setContent(mm);

			helper.setFrom(from);
			helper.setTo(to);
			if (cc != null && cc.length > 0) {
				helper.setCc(cc);
			}
			helper.setSubject(subject);
			javaMailSender.send(message);
		} catch (MailException e) {
			logger.info("Error to send mail !", e);
			throw e;
		} catch (MessagingException e) {
			logger.info("Error to send mail !", e);
			throw e;
		}
	}

	public void setJavaMailSender(JavaMailSender javaMailSender) {
		this.javaMailSender = javaMailSender;
	}

	public void setFrom(String from) {
		this.from = from;
	}
}
